package com.crm.Servlet;

import com.crm.Service.CustomerService;
import com.crm.ServiceImpl.CustomerServiceImpl;
import com.crm.pojo.Customer;
import com.crm.pojo.CustomerDetail;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

/**
 * @version : 1.0
 * @auther : hjx
 * @Date : 2021/6/28
 * @Description : 实训
 */
public class addCustomerServlet extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        resp.setContentType("text/html; charset=UTF-8");
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");

        String custName = req.getParameter("custName");
        String custSource = req.getParameter("custSource");
        String custIndustry = req.getParameter("custIndustry");
        String custLevel = req.getParameter("custLevel");
        String custPhone = req.getParameter("custPhone");
        String custMobile = req.getParameter("custMobile");

        String custRegion = req.getParameter("custRegion");
        String custMemo = req.getParameter("custMemo");
        String custAddress = req.getParameter("custAddress");
        String custCorporation = req.getParameter("custCorporation");
        String custFax = req.getParameter("custFax");
        String custWebsite = req.getParameter("custWebsite");
        String custLicence = req.getParameter("custLicence");
        String custPic = req.getParameter("custPic");
        long custCapital =  Long.valueOf(req.getParameter("custCapital"));
        String custBank = req.getParameter("custBank");
        int custId = 0;

        Customer customer = new Customer(custId,custName,custSource,custIndustry,custLevel,custPhone,custMobile);
        CustomerDetail custDetail = new CustomerDetail(custId,custRegion,custPic,custAddress,custFax,custWebsite,custLicence,custCorporation,custCapital,custBank,custPic,custMemo);

        CustomerService customerService = new CustomerServiceImpl();
        boolean b = false;
        try {
            b = customerService.addCustomer(customer, custDetail);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (b) {
            resp.sendRedirect("allCustomerServlet");
        } else {
            resp.sendRedirect("add.jsp");
        }
    }
}

